/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.action.processor;

import com.daikit.daikit4gxt.client.DkMain;
import com.daikit.daikit4gxt.client.action.BaseAction;
import com.daikit.daikit4gxt.client.ui.popup.MyMessageBox;
import com.daikit.daikit4gxt.client.ui.popup.MyMessageBox.AfterCloseListener;
import com.daikit.daikit4gxt.shared.bean.ConnectionReturnData;
import com.google.gwt.http.client.UrlBuilder;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Window.Location;


/**
 * Process {@link ConnectionReturnData}
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public class DkStandardConnectionReturnProcessor implements DkConnectionReturnProcessor
{

	@Override
	public void process(final BaseAction<?> currentChainAction, final ConnectionReturnData connectionReturn)
	{
		if (connectionReturn != null && connectionReturn.getRedirectURL() != null)
		{
			Window.Location.replace(connectionReturn.getRedirectURL());
		}
		else if (connectionReturn != null && connectionReturn.getNewLanguageIsocode() != null)
		{
			final UrlBuilder builder = Location.createUrlBuilder().setParameter("locale", connectionReturn.getNewLanguageIsocode());
			Window.Location.replace(builder.buildString());
		}
		else if (connectionReturn == null || connectionReturn.getUser() == null)
		{
			currentChainAction.stopChain();
			MyMessageBox.alert(DkMain.i18n().message_ConnectionFailed_title(), DkMain.i18n()
					.message_ConnectionFailed_badIdentifiers_label(), new AfterCloseListener()
			{
				@Override
				public void afterClose()
				{
					DkMain.controller().getConnectionPopupInstance().show();
				}
			});
		}
		else
		{
			DkMain.model().setUserLogged(connectionReturn.getUser());
		}
	}

}
